//
//  GainTopItem.swift
//  DSP
//
//  Created by tucici on 2024/5/17.
//

import UIKit
import TCCKit
let updateTopItemSlider = "updateTopItemSlider"
class GainTopItem: UICollectionViewCell {
    @IBOutlet var CH: UIButton!
    @IBOutlet var dB: UIButton!
    @IBOutlet var add: UIButton!
    @IBOutlet var sub: UIButton!
    @IBOutlet var mute: UIButton!
    @IBOutlet var temp: UIButton!
    private var slider : DSlider!
    private var isEdit : Bool = false
    var block: (()->Void)?
    var swipeBlock: (()->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        initUI()
        NotifyTool.add(self, #selector(updateAllMuteAction), updateAllMute)
        NotifyTool.add(self, #selector(updateSliderValue), updateTopItemSlider)
    }
    
    func initUI(){
        contentView.backgroundColor = .clear
        
        slider = DSlider()
        slider.delegate = self
        slider.minValue = -50
        slider.maxValue = 0
        slider.currentValue = 0
        slider.thumImg.image = UIImage(named: "slider_p")
        slider.setColors([HEX_RED.color(), HEX_RED.color()])
        slider.direction = .horizontal
        
        CH.contentEdgeInsets = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
        CH.titleLabel?.minimumScaleFactor = 0.8
        CH.titleLabel?.adjustsFontSizeToFitWidth = true
        
        contentView.addSubview(slider)
        slider.snp.makeConstraints { make in
            make.width.equalTo(30.k_h)
            make.centerX.equalToSuperview()
            make.top.equalTo(add.snp.bottom).offset(5.k_w)
            make.bottom.equalTo(sub.snp.top).offset(-5.k_w)
        }
    }
    
    func updateUI(){
        let title = model.Volume == model.minVolume ? "off" : "\(model.Volume)dB"
        dB.setTitle(title, for: .normal)
        CH.setTitle(model.name, for: .normal)
        temp.setTitle(model.phase ? "180°" : "0°", for: .normal)
        CH.titleLabel?.font = font(12, model.select && model.enable ? .bold : .regular)
        /* 以APP.CHM.enable为判断条件
         if !APP.CHM.enable{
         Tool.btnDisable(CH)
         Tool.btnDisable(dB)
         Tool.btnDisable(add)
         Tool.btnDisable(sub)
         Tool.btnDisable(mute)
         Tool.btnDisable(temp)
         slider.enable = false
         }else{
         model.select ? Tool.btnHightlight(CH) : Tool.btnNormal(CH)
         model.select ? Tool.btnHightlight(dB) : Tool.btnNormal(dB)
         model.select ? Tool.btnNormal(add) : Tool.btnDisable(add)
         model.select ? Tool.btnNormal(sub) : Tool.btnDisable(sub)
         
         !model.select ? Tool.btnDisable(mute) : (model.mute ? Tool.btnHightlight(mute) : Tool.btnNormal(mute))
         
         !model.select ? Tool.btnDisable(temp) : (model.phase ? Tool.btnHightlight(temp) : Tool.btnNormal(temp))
         
         slider.enable = model.select
         }
         */
        
        model.select ? Tool.btnHightlight(CH) : Tool.btnNormal(CH)
        model.select ? Tool.btnHightlight(dB) : Tool.btnNormal(dB)
        model.select ? Tool.btnNormal(add) : Tool.btnDisable(add)
        model.select ? Tool.btnNormal(sub) : Tool.btnDisable(sub)
        
        //mute 不熄灭，不以通道有没有选中来判断了
        //!model.select ? Tool.btnDisable(mute) : (model.mute ? Tool.btnHightlight(mute) : Tool.btnNormal(mute))
        model.mute ? Tool.btnHightlight(mute) : Tool.btnNormal(mute)
        
        !model.select ? Tool.btnDisable(temp) : (model.phase ? Tool.btnHightlight(temp) : Tool.btnNormal(temp))
        
        slider.enable = model.select
        
        
        
        if isEdit {return}
        slider.currentValue = CGFloat(model.Volume)
    }
    
    var  model = ChannelModel(){
        didSet{
            updateUI()
        }
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        switch sender{
        case CH:
            model.select = true
        case dB:
            break
        case sub:
            model.Volume = max(model.minVolume, model.Volume - 1)
        case add:
            model.Volume = min(model.maxVolume, model.Volume + 1)
        case mute:
            model.mute = !model.mute
        case temp:
            model.phase = !model.phase
        default:
            break
        }
        block?()
    }
    
    @objc func updateSliderValue(_ notify:Notification){
        guard let n = notify.object as? Int16 else{return}
        if !model.select{return}
        model.Volume = n
        updateUI()
    }
    @objc func updateAllMuteAction(){
        // model.Volume = APP.CHM.volume.int16
        //model.mute = APP.CHM.mute
        //if !model.select{return}
        // updateUI()
    }
    
}
//MARK: - DSliderDelgate
extension GainTopItem : DSliderDelgate{
    func valueForChange(_ sender: DSlider) {
        isEdit = true
        model.Volume = Int16(sender.currentValue)
        NotifyTool.post(updateTopItemSlider,model.Volume)
    }
    
    func valueForChangeEnd(_ sender: DSlider) {
        isEdit = false
        model.Volume = Int16(sender.currentValue)
        swipeBlock?()
    }
    
    func actionForInterval(_ sender: DSlider, _ time: Double) {
        swipeBlock?()
    }
}

